// $Id: base.js,v 1.8 2008/05/29 19:44:50 merlinofchaos Exp $
/**
 * @file base.js
 *
 * Some basic behaviors and utility functions for mewdkp.
 */

Drupal.mewdkp = {};

/**
 * jQuery UI tabs, mewdkp integration component
 */
Drupal.behaviors.mewdkpTabs = function (context) {
  if ($.ui && $.ui.tabs) {
    $('#mewdkp-tabset:not(.mewdkp-processed)').addClass('mewdkp-processed').tabs({
      selectedClass: 'active'
    });
  }

  $('a.mewdkp-remove-link')
    .addClass('mewdkp-processed')
    .click(function() {
      var id = $(this).attr('id').replace('mewdkp-remove-link-', '');
      $('#mewdkp-row-' + id).hide();
      $('#mewdkp-removed-' + id).attr('checked', true);
      return false;
    });
}

/**
 * For IE, attach some javascript so that our hovers do what they're supposed
 * to do.
 */
Drupal.behaviors.mewdkpHoverlinks = function() {
  if ($.browser.msie) {
    // If IE, attach a hover event so we can see our admin links.
    $("div.view:not(.mewdkp-hover-processed)").addClass('mewdkp-hover-processed').hover(
      function() {
        $('div.mewdkp-hide', this).addClass("mewdkp-hide-hover"); return true;
      },
      function(){
        $('div.mewdkp-hide', this).removeClass("mewdkp-hide-hover"); return true;
      }
    );
    $("div.mewdkp-admin-links:not(.mewdkp-hover-processed)")
      .addClass('mewdkp-hover-processed')
      .hover(
        function() {
          $(this).addClass("mewdkp-admin-links-hover"); return true;
        },
        function(){
          $(this).removeClass("mewdkp-admin-links-hover"); return true;
        }
      );
  }
}

/**
 * Helper function to parse a querystring.
 */
Drupal.mewdkp.parseQueryString = function (query) {
  var args = {};
  var pos = query.indexOf('?');
  if (pos != -1) {
    query = query.substring(pos + 1);
  }
  var pairs = query.split('&');
  for(var i in pairs) {
    var pair = pairs[i].split('=');
    // Ignore the 'q' path argument, if present.
    if (pair[0] != 'q' && pair[1]) {
      args[pair[0]] = unescape(pair[1].replace(/\+/g, ' '));
    }
  }
  return args;
};

